<?php
$db ='etat-gurdeep';
$db_host = 'localhost';
$db_user ='root';
$db_password = '';

$conn = mysql_connect($db_host,$db_user,$db_password) or die("Could Not Connect to DB".mysql_error());
$select_db = mysql_select_db($db,$conn);
//$access_level = 0;
//$script = 'survey';
//require_once '../../controller.php';
require_once '../../classes/Survey.php';
require_once '../../classes/Question.php';
require_once '../../classes/Response.php';
require_once '../../classes/Student.php';

date_default_timezone_set('America/Los_Angeles');
if(isset($_REQUEST['sid'])&& isset($_REQUEST['mid'])){
	$survey_id = $_REQUEST['sid'];
	$student_id = $_REQUEST['mid'];

if(isset($_REQUEST['sec'])){
	$sec = $_REQUEST['sec'];
}else{
	$sec =1;
}

if(isset($_REQUEST['num'])){
	$q_num = $_REQUEST['num'];
}else{
	$q_num =0;
}
session_start();
$s = new Survey();
$q = new Question();
$r = new Response();
$st = new Student($student_id);
$student_name = $st->full_name;
$questions = $s->getQuestions($survey_id);

if(isset($_POST['response'])){
	if(!isset($_SESSION['response_id'])){
	if($sec==1){
	$timestamp = date('Y-m-d H:i:s');
	$data = array(
	               'survey_id'=>$survey_id,
	               'student_id'=>$student_id,
	               'student_name'=>$student_name,
	               'timestamp'=>$timestamp
	);        
	
	$r->saveResponse($data);
	}
    $response_id = $r->getLatestResponse($student_id);
     $_SESSION['response_id']=$response_id;   
	}
    foreach($questions as $i=>$value){
    	$ques_id = $value->id;
    	if(isset($_SESSION['response_id'])){
    		$response_id=$_SESSION['response_id'];
    	}
        if(isset($_POST['response_'.$i])) {
            $response = addslashes($_POST['response_'.$i]);
            if(isset($_POST['comment_'.$i])){
            	$comment = addslashes($_POST['comment_'.$i]);
            }else{
            	$comment='';
            }
            $response_data = array('response_id'=>$response_id,'survey_id'=>$survey_id,'question_id'=>$ques_id,'response'=>$response,'comment'=>$comment);
            if($r->checkResponse($response_id,$ques_id)){
             $r->updateResponseValues($response_data);	
            }else{
            $r->saveResponseValues($response_data);   
            }         
          }

        if(isset($_POST['response_check1_'.$i])|| isset($_POST['response_check2_'.$i]) || isset($_POST['response_check3_'.$i]) || isset($_POST['response_check4_'.$i]) || isset($_POST['response_check5_'.$i])|| isset($_POST['response_check6_'.$i])) {
            $response = $_POST['response_check1_'.$i].'+'.$_POST['response_check2_'.$i].'+'.$_POST['response_check3_'.$i].'+'.$_POST['response_check4_'.$i].'+'.$_POST['response_check5_'.$i].'+'.$_POST['response_check6_'.$i];
            $response = addslashes($response);
           if(isset($_POST['comment_'.$i])){
            	$comment = addslashes($_POST['comment_'.$i]);
            }else{
            	$comment='';
            }
            $response_data = array('response_id'=>$response_id,'survey_id'=>$survey_id,'question_id'=>$ques_id,'response'=>$response,'comment'=>$comment);
            if($r->checkResponse($response_id,$ques_id)){
             $r->updateResponseValues($response_data);	
            }else{
            $r->saveResponseValues($response_data);   
            }
          }

        if(isset($_POST['response_rate1_'.$i])|| isset($_POST['response_rate2_'.$i]) || isset($_POST['response_rate3_'.$i]) || isset($_POST['response_rate4_'.$i]) || isset($_POST['response_rate5_'.$i])) {
            $response = $_POST['response_rate1_'.$i].'+'.$_POST['response_rate2_'.$i].'+'.$_POST['response_rate3_'.$i].'+'.$_POST['response_rate4_'.$i].'+'.$_POST['response_rate5_'.$i];
            $response=addslashes($response);
            if(isset($_POST['comment_'.$i])){
            	$comment = addslashes($_POST['comment_'.$i]);
            }else{
            	$comment='';
            }
            $response_data = array('response_id'=>$response_id,'survey_id'=>$survey_id,'question_id'=>$ques_id,'response'=>$response,'comment'=>$comment);
            if($r->checkResponse($response_id,$ques_id)){
             $r->updateResponseValues($response_data);	
            }else{
            $r->saveResponseValues($response_data);   
            }
          }
         
    }

$sec = $_POST['section_number'];
$q_num = $_POST['question_number'];
if(!isset($_POST['end_survey'])){
header("location: publicsurvey.php?sid=$survey_id&mid=$student_id&sec=$sec&num=$q_num");
}else{
	unset($_SESSION['response_id']);
	session_destroy();
	header("location: endsurvey.php?sid=$survey_id");
}
}
$survey = $s->getSurvey($survey_id);


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<title>eTAT|Public Survey</title>
<link href='../templates/template1.css' rel='stylesheet' type='text/css'></link>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
  $(document).ready(function() {
	  $("#datepicker").datepicker();
	  $.datepicker.formatDate('yy-mm-dd',new Date(2007, 1 - 1, 26));
    
  });
  </script>
<script language='JavaScript' type='text/javascript' src='render_survey_script.js'></script>
</head>
<body>
<div id="display-survey">
<?php 


echo "<h1 align='center'>".$survey['title']."</h1>";
if($sec==1){
echo "<p align='center'>".$survey['welcome_message']."</p>";
}

echo '<br><br>';

echo '( Questions marked <font color="red" size="5">*</font> are required )<br><br>';

echo '<form name="public_survey" method="post" onsubmit="return validateAll();">';

if($questions){
	
	$size = sizeof($questions);
	$num = $q_num-$sec+2;
	foreach($questions as $i=>$value){
		$met_condition = true;
		$id = $value->id;
		$type = $value->type;
		$required = $value->required;
		$question = $value->question;
		$create_section = $value->create_section;
		$order = $value->order;
		$comment_box = $value->comment_box;
		$response1 = $value->response1;
		$response2 = $value->response2;
		$response3 = $value->response3;
		$response4 = $value->response4;
		$response5 = $value->response5;
		$response6 = $value->response6;

		if($q->checkCondition($id)){
			$conditions = $q->checkCondition($id);
			foreach($conditions as $c=>$values){
				foreach($values as $key=>$value){
				if($key == 'question_2_id'){
		            $question_2_id = $value;	
	               }	
	            if($key == 'condition'){
	             	$condition = $value;	
	              }
	            if($key == 'condition_value'){
	            	$condition_value = $value;	
	              }
				}

				$q2_order = $q->getOrder($question_2_id);
	             if($q2_order <$order){ 
				$question_2_response = $r->getResponseValue($_SESSION['response_id'],$question_2_id);	
			
				if($condition =='equal'){
				if($condition_value ==$question_2_response){
					$met_condition = true;
					$condition_flag=true;
				}else{
					$met_condition = false;
				}	
				}elseif($condition =='less'){
				if($condition_value < $question_2_response){
					$met_condition = true;
					$condition_flag=true;
				}else{
					$met_condition = false;
				}
				}elseif($condition =='greater'){
				if($condition_value >$question_2_response){
					$met_condition = true;
					$condition_flag=true;
				}else{
					$met_condition = false;
				}
				}else{
					$met_condition = false	;
			    }
			}	
		
		}	
		}	
		
		if($q_num<$i || $q_num==$i){
		if($met_condition==true){
			
		if($required=='Y'){
			$class='required';
		}else{
			$class='not-required';
		}	
			
		if($create_section =="Y" && $i!=$q_num){
			$sec++ ;$q_num=$i;

			echo "<input type='hidden' name='section_number' value='$sec'";
			echo "<input type='hidden' name='question_number' value='$q_num'";
		   if($sec>2){
			echo "<div id='back'><input type='button' onclick='history.go(-1)' Value='Back'></div>";
			}
			echo "<div id='next'><input type='submit' name='response' value='Next'></div>";
			echo "</form>";
			break;
		}
		if($type=='Section Text'){
			echo '<p align="center">'.$question.'</p><br>';
			$num--;
		}elseif($type =='Text'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo "<input type='text' class='$class' name='response_$i' size = '50px'><br>";
		}elseif($type=='Date'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo "<input type='text' class='$class' title='Enter Date Only' name='response_$i' size = '15px'><br>";
		if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		echo "<br>";    
		}elseif($type=='Numeric'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo "<input type='text' class='$class' id='numeric_$i' title='Enter Number Only' name='response_$i' size = '15px'><br>";
		if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		echo "<br>";    
		}elseif($type=='Yes/No'){
		    if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo "<input type='radio' class='$class' name='response_$i' value='Yes'>Yes<br>";
			echo "<input type='radio' class='$class' name='response_$i' value='No'>No<br>";
		if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		    echo "<br>";
		}elseif($type=='Check Box'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			if($response1!='')
			echo " <input type='checkbox' class = '$class' name='response_check1_$i' value ='$response1'>$response1<br>";
			if($response2!='')
			echo " <input type='checkbox' class = '$class' name='response_check2_$i' value ='$response2'>$response2<br>";
			if($response3!='')
			echo " <input type='checkbox' class = '$class' name='response_check3_$i' value ='$response3'>$response3<br>";
			if($response4!='')
			echo " <input type='checkbox' class = '$class' name='response_check4_$i' value ='$response4'>$response4<br>";
			if($response5!='')
			echo " <input type='checkbox' class = '$class' name='response_check5_$i' value ='$response5'>$response5<br>";
			if($response6!='')
			echo " <input type='checkbox' class = '$class' name='response_check6_$i' value ='$response6'>$response6<br>";
		if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		 echo "<br>";   
		}elseif($type=='Radio Button'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			
			if($response1!='')
            echo " <input type='radio' class = '$class' name='response_$i' value ='$response1'>$response1<br>";
		    if($response2!='')
			echo " <input type='radio' class = '$class' name='response_$i' value ='$response2'>$response2<br>";
			if($response3!='')
			echo " <input type='radio' class = '$class' name='response_$i' value ='$response3'>$response3<br>";
			if($response4!='')
			echo " <input type='radio' class = '$class' name='response_$i' value ='$response4'>$response4<br>";
			if($response5!='')
			echo " <input type='radio' class = '$class' name='response_$i' value ='$response5'>$response5<br>";
			if($response6!='')			
			echo " <input type='radio' class = '$class' name='response_$i' value ='$response6'>$response6<br>";
			if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }	
		    echo "<br>";
		}elseif($type=='Drop Down'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo"<select name='response_$i' class='$class'>";
			echo '<option selected="selected" value="0">Choose One</option>';
			if($response1!='')
			echo '<option>'.$response1.'</option>';
			if($response2!='')
			echo '<option>'.$response2.'</option>';
			if($response3!='')
			echo '<option>'.$response3.'</option>';
			if($response4!='')
			echo '<option>'.$response4.'</option>';
			if($response5!='')
			echo '<option>'.$response5.'</option>';
			if($response6!='')
			echo '<option>'.$response6.'</option>';
			echo '</select><br>';
		if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		    echo "<br>";
		}elseif($type=='Rate'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo '<table>';
			
			echo '<tr><td width="70px">Value</td><td>&nbsp;</td><td>1</td><td>&nbsp;</td><td>2</td><td>&nbsp;</td><td>3</td><td>&nbsp;</td><td>4</td><td>&nbsp;</td><td>5</td></tr>';
			if($response1!='')
            echo "<tr><td>$response1</td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate1_$i' value ='res1-1'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate1_$i' value ='res1-2'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate1_$i' value ='res1-3'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate1_$i' value ='res1-4'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate1_$i' value ='res1-5'></td></tr>";
		    if($response2!='')
            echo "<tr><td>$response2</td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate2_$i' value ='res2-1'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate2_$i' value ='res2-2'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate2_$i' value ='res2-3'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate2_$i' value ='res2-4'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate2_$i' value ='res2-5'></td></tr>";			
            if($response3!='')
            echo "<tr><td>$response3</td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate3_$i' value ='res3-1'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate3_$i' value ='res3-2'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate3_$i' value ='res3-3'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate3_$i' value ='res3-4'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate3_$i' value ='res3-5'></td></tr>";
            if($response4!='')
            echo "<tr><td>$response4</td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate4_$i' value ='res4-1'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate4_$i' value ='res4-2'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate4_$i' value ='res4-3'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate4_$i' value ='res4-4'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate4_$i' value ='res4-5'></td></tr>";
            if($response5!='')
            echo "<tr><td>$response5</td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate5_$i' value ='res5-1'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate5_$i' value ='res5-2'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate5_$i' value ='res5-3'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate5_$i' value ='res5-4'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate5_$i' value ='res5-5'></td></tr>";
         	if($response6!='')
            echo "<tr><td>$response6</td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate6_$i' value ='res6-1'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate6_$i' value ='res6-2'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate6_$i' value ='res6-3'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate6_$i' value ='res6-4'></td><td>&nbsp;</td><td><input type='radio' class = '$class' name='response_rate6_$i' value ='res6-5'></td></tr>";			
            echo '</table><br>';
		if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		 echo "<br>";   
		}elseif($type=='Likert Scale'){
			if($required=='Y')
			echo '<font color="red" size="5">*</font>';
			echo '<b>'.$num.'. '.$question.'</b><br>';
			echo " <input type='radio' class = '$class' name='response_$i' value ='Strongly Agree'>Strongly Agree<br>";
		    echo " <input type='radio' class = '$class' name='response_$i' value ='Agree'>Agree<br>";
			echo " <input type='radio' class = '$class' name='response_$i' value ='Neutral'>Neutral<br>";
			echo " <input type='radio' class = '$class' name='response_$i' value ='Disagree'>Disagree<br>";
		    echo " <input type='radio' class = '$class' name='response_$i' value ='Strongly Disagree'>Disagree<br>";
		    if($comment_box=='Y'){
			echo "<br>";
			echo "<a href='javascript:showCommentBox($i);'><font style='align:right' color='blue'><u>Add Comments</u></font></a><br>";
		    echo"<textarea cols='30' rows='2' style='visibility:hidden' id='$i' name='comment_$i'></textarea><br>";
		    }
		    echo "<br>";
		   }elseif($type=='Descriptive Text'){
			echo '<b>'.$question.'</b><br>';
			$num--;
			}
	echo'<br>';
		}
	$num++;
		
	}
	if($size==$i+1){
		$sec++ ;$q_num=$i+1;
		echo "<input type='hidden' name='section_number' value='$sec'";
		echo "<input type='hidden' name='question_number' value='$q_num'";
		echo '<input type="hidden" name="end_survey">';
	    if($sec>2){
		echo "<div id='back'><input type='button' onclick='history.go(-1)' Value='Back'></div>";
		}
		echo '<div id="next"><input type="submit" name="response" value="Submit Survey"></div>';
		
		echo '</form>';
	}
	}
}

?>
</div>
</body>
</html>

<?php 
}else{
	echo '<h1>. No Survey Exists. Please check if the link is typed correctly</h1>';
}

?>
